package org.project.stone;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;
import java.nio.charset.Charset;
import java.util.Set;

public class MultiplexingDemo {
    public static void main(String[] args) throws IOException {
        Selector selector = Selector.open();

        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        serverSocketChannel.configureBlocking(false);
        serverSocketChannel.bind(new InetSocketAddress(8090));


        SelectionKey register = serverSocketChannel.register(selector, SelectionKey.OP_ACCEPT);

        System.out.println("系统准备好了");


        while (true){
            int tmp = selector.select();

            Set<SelectionKey> selectionKeys = selector.selectedKeys();
            System.out.println(selectionKeys.size());

            selectionKeys.stream()
                    .forEach(ele->{
                        if (ele.isAcceptable()){
                            System.out.println("可接受");

                            ServerSocketChannel channel = (ServerSocketChannel) (ele.channel());
                            try {
                                SocketChannel accept = channel.accept();
                                if (accept != null){
                                    accept.configureBlocking(false);
                                    accept.register(selector,SelectionKey.OP_ACCEPT);
                                    accept.register(selector,SelectionKey.OP_CONNECT);
                                    accept.register(selector,SelectionKey.OP_READ);
                                    accept.register(selector,SelectionKey.OP_WRITE);
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        }else if (ele.isConnectable()){
                            System.out.println("连接");
                        }else if (ele.isReadable()){
                            SocketChannel channel = (SocketChannel) (ele.channel());
                            try {
                                ByteBuffer allocate = ByteBuffer.allocate(1024);
                                int read = channel.read(allocate);
                                if (read != -1){
                                    allocate.flip();
                                    System.out.println("可读事件传输了"+read+"字符");
                                    CharBuffer str = Charset.forName("UTF-8").decode(allocate);
                                    System.out.println(str);
                                }else {
                                    ele.cancel();
                                }
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                            System.out.println("可读事件");
                        }else if (ele.isValid()){
                            System.out.println("验证事件");
                        }else if (ele.isWritable()){
                            System.out.println("可写事件");
                        }
                    });
        }
    }
}
